import Request from './request'
import { getToken } from '../auth'
import store from '@store/index'
const whiteLoading = []
const whiteShowErr = []
const whiteToken = []
const http = new Request()
const baseUrl = `${process.env.VUE_APP_BASE_URL}`
// const baseUrl1 = `${process.env.VUE_APP_BASE_URL1}`
/* 设置全局配置 */
http.setConfig((config) => {
    config.baseUrl = baseUrl /* 根域名不同 */
    // config.header = {
    //   ...config.header,
    // }
    return config
})

/**
 * 自定义验证器，如果返回true 则进入响应拦截器的响应成功函数(resolve)，否则进入响应拦截器的响应错误函数(reject)
 * @param { Number } statusCode - 请求响应体statusCode（只读）
 * @return { Boolean } 如果为true,则 resolve, 否则 reject
 */
// 有默认，非必写
http.validateStatus = (statusCode) => {
    return statusCode === 200
}
/* 请求之前拦截器 */
http.interceptor.request((config) => {
    const { loading = true, notAuth = false } = config.extraConfig || {}
    if (getToken() && !notAuth) {
        config.header.Token = getToken()
    } else {
        delete config.header.Token
    }
    // 开启请求弹框
    if (loading) {
        uni.showLoading({
            title: '请稍后...',
            mask: true
        })
    }
    return config
})

/* 请求之后拦截器 */
// 必须使用异步函数，注意
http.interceptor.response(async (response) => {
    // 关闭请求弹框
    if (whiteLoading.indexOf(response.config.url) < 0) {
        uni.hideLoading()
    }
    // console.log(response.statusCode)
    if (response.statusCode === 200) {
        const { code, msg } = response.data
        // if (code !== 200 && whiteShowErr.indexOf(response.config.url) < 0) {
        //     // Utils.toast(msg)
        // }
        if (code == 401) {
            uni.hideLoading()
            console.log(99999, response.data)
            uni.clearStorageSync('token')
            store.commit('baseStore/SET_USERINFO', {})
            uni.showToast({
                icon: 'none',
                title: response.data.msg,
                duration: 2000
            })
            setTimeout(function () {
                uni.reLaunch({
                    url: '/pages/login/checkUser'
                })
            }, 2000)
        }
        return Promise.resolve(response.data)
    } else {
        return Promise.reject(response)
    }
}, (response, resolve, reject) => {
    console.log(99988877766, response.statusCode)
    uni.hideLoading()
    if (response.statusCode) {
        let errMsg = ''
        switch (response.statusCode) {
            case 400:
                errMsg = '请求错误'
                break
            case 401:
                errMsg = '未授权，请登录'
                break
            case 403:
                errMsg = '拒绝访问'
                break
            case 404:
                errMsg = `请求地址出错: ${response.config.url}`
                break
            case 405:
                errMsg = '请求方法不支持'
                break
            case 408:
                errMsg = '请求超时'
                break
            case 500:
                if (response.data.msg) {
                    errMsg = response.data.msg
                } else {
                    errMsg = '网络繁忙请稍后'
                }
                break
            case 501:
                errMsg = '服务未实现'
                break
            case 502:
                errMsg = '网关错误'
                break
            case 503:
                errMsg = '服务不可用'
                break
            case 504:
                errMsg = '网关超时'
                break
            case 505:
                errMsg = 'HTTP版本不受支持'
                break
            default:
                errMsg = '请求未知错误'
                break
        }
        // if(response.config.noTost){
        //     return
        // }

        if (errMsg) {
            uni.showToast(Object.assign({
                icon: 'none',
                title: errMsg,
                duration: 2000
            }))
        }
    } else {
        // if(response.config.noTost){
        //     return
        // }
        // Utils.toast('请求未知失败')
    }
    reject(response)
})

export default http
